package com.ecgsystem.services;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.StringTokenizer;

import com.ecgsystem.constants.Commons;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class GetOnlinePatientsService extends Service {

	private static final String TAG = "BroadcastService";
	public static final String BROADCAST_ACTION = "com.ecgsystem2b.activity.displayevent";
	private final Handler handler = new Handler();
	Intent intent;
	private InetAddress serverAddr;
	private String receivedMessage;
	private StringTokenizer receivedMess;

	private PrintWriter out;
	private BufferedReader in;
	private Socket socket;
	String onlinePatients[];
	int count = 0;

	@Override
	public void onCreate() {
		super.onCreate();

		try {
			serverAddr = InetAddress.getByName(Commons.UDP_SERVER_ADDRESS);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		onlinePatients = new String[100];
		intent = new Intent(BROADCAST_ACTION);
	}

	@Override
	public void onStart(Intent intent, int startId) {
		handler.removeCallbacks(sendUpdatesToUI);
		handler.postDelayed(sendUpdatesToUI, 1000); // 1 second

	}

	private Runnable sendUpdatesToUI = new Runnable() {
		public void run() {
			DisplayLoggingInfo();
			handler.postDelayed(this, 10000); // 10 seconds
		}
	};

	private void DisplayLoggingInfo() {
		Log.d(TAG, "entered DisplayLoggingInfo");

		try {
			// create a socket to make the connection with the server
			socket = new Socket(serverAddr, Commons.TCP_PORT);

			out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
					socket.getOutputStream())), true);

			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));

			out.print("RequestOnlineList");
			out.flush();
			receivedMessage = in.readLine();

			if (receivedMessage != null) {

				receivedMess = new StringTokenizer(receivedMessage, "|#");

				count = 0;

				// lay ra danh sach benh nhan
				while (receivedMess.hasMoreTokens()) {
					onlinePatients[count] = receivedMess.nextToken();
					count++;
				}

				socket.close();
			}
		} catch (IOException e) {
			count = 0;
			e.printStackTrace();
		} finally {
			intent.putExtra("COUNT", count);
			intent.putExtra("LIST", onlinePatients);
		}

		sendBroadcast(intent);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onDestroy() {
		handler.removeCallbacks(sendUpdatesToUI);
		try {
			if (socket != null)
				socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.onDestroy();
	}
}
